﻿namespace ExamV5.CheckPaper
{
    using ExamV5;
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class CheckPaperList : Page
    {
        protected Button butQuery;
        protected GridView DataGridInfo;
        protected DropDownList DDLQuery;
        protected DropDownList DDLSubject;
        protected HtmlForm Form1;
        protected HtmlHead Head1;
        private HttpCookie httpCookie_0 = null;
        private HttpCookie httpCookie_1 = null;
        private HttpCookie httpCookie_2 = null;
        private HttpCookie httpCookie_3 = null;
        protected Label labRescordCount;
        protected LinkButton LBFirstPage;
        protected LinkButton LBLastPage;
        protected LinkButton LBNextPage;
        protected LinkButton LBPrevPage;
        private PublicFun publicFun_0 = new PublicFun();
        protected ScriptManager SM;
        protected TextBox txtQuery;
        protected UpdatePanel UPPart;

        protected void butQuery_Click(object sender, EventArgs e)
        {
            this.DataGridInfo.PageIndex = 0;
            this.method_0();
        }

        protected void DataGridInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.DataGridInfo.PageIndex = e.NewPageIndex;
            this.method_0();
        }

        protected void DataGridInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItemIndex == -1)
            {
                this.DataGridInfo.Attributes.Add("oldValue", "#FFFFFF");
                this.DataGridInfo.Attributes.Add("singleValue", "#FFFFFF");
            }
            else
            {
                e.Row.Attributes.Add("onmouseover", "this.bgColor='#F6F5F5'");
                if ((e.Row.DataItemIndex % 2) != 0)
                {
                    e.Row.Attributes.Add("bgcolor", "#FFFFFF");
                    e.Row.Attributes.Add("onmouseout", "this.bgColor=document.getElementById('DataGridInfo').getAttribute('oldValue')");
                }
                else
                {
                    e.Row.Attributes.Add("bgcolor", "#FFFFFF");
                    e.Row.Attributes.Add("onmouseout", "this.bgColor=document.getElementById('DataGridInfo').getAttribute('singleValue')");
                }
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = "******";
                if (e.Row.Cells[2].Text.Length > 10)
                {
                    e.Row.Cells[2].Text = "<a title=" + e.Row.Cells[2].Text + ">" + e.Row.Cells[2].Text.Substring(0, 10) + "…</a>";
                }
                Label label = (Label) e.Row.Cells[6].FindControl("labCheck");
                if (e.Row.Cells[4].Text.Trim() == "B")
                {
                    e.Row.Cells[4].Text = "<font color=red>批改中</font>";
                    if (e.Row.Cells[5].Text.Trim() == this.httpCookie_0.Value)
                    {
                        label.Text = "<a href='#' onclick=javascript:CheckPapering('" + this.DataGridInfo.DataKeys[e.Row.RowIndex]["PaperID"].ToString() + "','" + base.Server.UrlEncode(this.DataGridInfo.DataKeys[e.Row.RowIndex]["UserCode"].ToString()) + "')>" + label.Text + "</a>";
                    }
                }
                else
                {
                    e.Row.Cells[4].Text = "<font color=green>空闲中</font>";
                    label.Text = "<a href='#' onclick=javascript:CheckPapering('" + this.DataGridInfo.DataKeys[e.Row.RowIndex]["PaperID"].ToString() + "','" + base.Server.UrlEncode(this.DataGridInfo.DataKeys[e.Row.RowIndex]["UserCode"].ToString()) + "')>" + label.Text + "</a>";
                }
            }
            if (e.Row.RowType == DataControlRowType.Header)
            {
                if (this.ViewState["SortType"].ToString() != " Desc")
                {
                    switch (this.ViewState["SortFiled"].ToString())
                    {
                        case "ExamName":
                            e.Row.Cells[1].Controls.Add(new LiteralControl("↓"));
                            break;

                        case "SubjectName":
                            e.Row.Cells[2].Controls.Add(new LiteralControl("↓"));
                            break;

                        case "PaperFen":
                            e.Row.Cells[3].Controls.Add(new LiteralControl("↓"));
                            break;

                        case "CheckStatus":
                            e.Row.Cells[4].Controls.Add(new LiteralControl("↓"));
                            break;
                    }
                }
                else
                {
                    switch (this.ViewState["SortFiled"].ToString())
                    {
                        case "ExamName":
                            e.Row.Cells[1].Controls.Add(new LiteralControl("↑"));
                            break;

                        case "SubjectName":
                            e.Row.Cells[2].Controls.Add(new LiteralControl("↑"));
                            break;

                        case "PaperFen":
                            e.Row.Cells[3].Controls.Add(new LiteralControl("↑"));
                            break;

                        case "CheckStatus":
                            e.Row.Cells[4].Controls.Add(new LiteralControl("↑"));
                            break;
                    }
                }
            }
        }

        protected void DataGridInfo_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sortExpression = e.SortExpression;
            if (!(this.ViewState["SortFiled"].ToString() == sortExpression))
            {
                this.ViewState["SortFiled"] = e.SortExpression;
            }
            else if (this.ViewState["SortType"].ToString() == " Desc")
            {
                this.ViewState["SortType"] = " ASC";
            }
            else
            {
                this.ViewState["SortType"] = " Desc";
            }
            this.method_0();
        }

        protected void DDLSubject_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.DataGridInfo.PageIndex = 0;
            this.method_0();
        }

        protected void LBFirstPage_Click(object sender, EventArgs e)
        {
            this.DataGridInfo.PageIndex = 0;
            this.method_0();
        }

        protected void LBLastPage_Click(object sender, EventArgs e)
        {
            if (this.DataGridInfo.PageCount > 0)
            {
                this.DataGridInfo.PageIndex = this.DataGridInfo.PageCount - 1;
                this.method_0();
            }
        }

        protected void LBNextPage_Click(object sender, EventArgs e)
        {
            if (this.DataGridInfo.PageIndex < (this.DataGridInfo.PageCount - 1))
            {
                this.DataGridInfo.PageIndex++;
                this.method_0();
            }
        }

        protected void LBPrevPage_Click(object sender, EventArgs e)
        {
            if (this.DataGridInfo.PageIndex > 0)
            {
                this.DataGridInfo.PageIndex--;
                this.method_0();
            }
        }

        private void method_0()
        {
            string str = "";
            string str2 = "";
            string str3 = "";
            string str4 = "";
            string str5 = "";
            string str6 = "";
            string str7 = "";
            string selectCommandText = "";
            string str9 = "";
            string connectionString = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString;
            SqlConnection connection = new SqlConnection(connectionString);
            connection.Open();
            SqlCommand command = new SqlCommand("select CheckPaperID,CheckRubricF,CheckRubricO,CheckRubricC,CheckRubricT,CheckRubricA,CheckRubricAN,CheckRubricJ  from UserPower where UserCode='" + this.httpCookie_0.Value + "'", connection);
            SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
            if (reader.Read())
            {
                str = reader["CheckRubricF"].ToString();
                str2 = reader["CheckRubricA"].ToString();
                str3 = reader["CheckRubricC"].ToString();
                str4 = reader["CheckRubricT"].ToString();
                str5 = reader["CheckRubricAN"].ToString();
                str6 = reader["CheckRubricO"].ToString();
                str7 = reader["CheckRubricJ"].ToString();
                str9 = reader["CheckPaperID"].ToString();
            }
            reader.Close();
            command.Dispose();
            connection.Close();
            connection.Dispose();
            if (((((str == "Y") || (str2 == "Y")) || ((str3 == "Y") || (str4 == "Y"))) || ((str5 == "Y") || (str6 == "Y"))) || (str7 == "Y"))
            {
                string str11 = "";
                selectCommandText = "select a.LastCheckUser,a.CheckStatus,a.UserCode,a.PaperID,b.ExamName,b.SubjectName,b.PaperFen from PaperInfo b,[CheckPaper] a,ExamUser c where c.PaperID=a.PaperID and (c.Flag='C' or c.Flag='D')  and c.UserCode=a.UserCode and  b.PaperStatus='A'  and a.PaperID=b.ID ";
                if (str == "Y")
                {
                    if (!(str11 == ""))
                    {
                        str11 = str11 + " or (a.CheckUserF='还未批改' and a.RubricFOK not  like  '')";
                    }
                    else
                    {
                        str11 = " and ( (a.CheckUserF='还未批改' and a.RubricFOK not  like  '')";
                    }
                }
                if (str2 == "Y")
                {
                    if (str11 == "")
                    {
                        str11 = " and ( (a.CheckUserA='还未批改' and a.RubricAOK not  like  '')";
                    }
                    else
                    {
                        str11 = str11 + " or (a.CheckUserA='还未批改' and a.RubricAOK not  like  '')";
                    }
                }
                if (str3 == "Y")
                {
                    if (!(str11 == ""))
                    {
                        str11 = str11 + " or (a.CheckUserC='还未批改' and a.RubricCOK not  like  '')";
                    }
                    else
                    {
                        str11 = " and ( (a.CheckUserC='还未批改' and a.RubricCOK not  like  '')";
                    }
                }
                if (str4 == "Y")
                {
                    if (!(str11 == ""))
                    {
                        str11 = str11 + " or (a.CheckUserT='还未批改' and a.RubricTOK not  like  '')";
                    }
                    else
                    {
                        str11 = " and ( (a.CheckUserT='还未批改' and a.RubricTOK not  like  '')";
                    }
                }
                if (str5 == "Y")
                {
                    if (!(str11 == ""))
                    {
                        str11 = str11 + " or (a.CheckUserAN='还未批改' and a.RubricANOK not  like  '')";
                    }
                    else
                    {
                        str11 = " and ( (a.CheckUserAN='还未批改' and a.RubricANOK not  like  '')";
                    }
                }
                if (str6 == "Y")
                {
                    if (str11 == "")
                    {
                        str11 = " and ( (a.CheckUserO='还未批改' and a.RubricOOK not  like  '')";
                    }
                    else
                    {
                        str11 = str11 + " or (a.CheckUserO='还未批改' and a.RubricOOK not  like  '')";
                    }
                }
                if (str7 == "Y")
                {
                    if (!(str11 == ""))
                    {
                        str11 = str11 + " or (a.CheckUserJ='还未批改' and a.RubricJOK not  like  '')";
                    }
                    else
                    {
                        str11 = " and ( (a.CheckUserJ='还未批改' and a.RubricJOK not  like  '')";
                    }
                }
                str11 = str11 + ")";
                selectCommandText = selectCommandText + str11;
                if (this.DDLSubject.SelectedItem.Value != "0")
                {
                    selectCommandText = selectCommandText + " and b.SubjectName like '%" + this.publicFun_0.CheckString(this.DDLSubject.SelectedItem.Text.Trim().Replace("│", "").Replace("├", "")) + "%'";
                }
                if (this.txtQuery.Text.Trim() != "")
                {
                    selectCommandText = selectCommandText + " and " + this.publicFun_0.CheckString(this.DDLQuery.SelectedItem.Value) + " like '%" + this.publicFun_0.CheckString(this.txtQuery.Text) + "%'";
                }
                if (str9.Trim() != "")
                {
                    str9 = str9.Substring(1, str9.Trim().Length - 2);
                    str9 = this.publicFun_0.CheckString(str9);
                    str9 = "'" + str9.Replace(",", "','") + "'";
                    selectCommandText = selectCommandText + " and b.ID in(" + str9 + ")";
                }
                selectCommandText = selectCommandText + " order by " + this.ViewState["SortFiled"].ToString() + this.ViewState["SortType"].ToString();
            }
            else
            {
                ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheckA", "alert('批改试卷权限未开放！')", true);
                selectCommandText = "select a.LastCheckUser,a.CheckStatus,a.UserCode,a.PaperID,b.ExamName,b.SubjectName,b.PaperFen from PaperInfo b,[CheckPaper] a,ExamUser c where c.PaperID=a.PaperID and (c.Flag='C' or c.Flag='D')  and c.UserCode=a.UserCode and  b.PaperStatus='A'  and a.PaperID=b.ID and b.ID=0";
            }
            SqlConnection selectConnection = new SqlConnection(connectionString);
            SqlDataAdapter adapter = new SqlDataAdapter(selectCommandText, selectConnection);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet, "DataInfo");
            this.DataGridInfo.DataSource = dataSet.Tables["DataInfo"].DefaultView;
            this.DataGridInfo.DataBind();
            this.labRescordCount.Text = "总共&nbsp;<font color='#0000C0'><b>" + dataSet.Tables["DataInfo"].Rows.Count.ToString() + "</b></font>&nbsp;条记录&nbsp;<font color='#0000C0'><b>" + Convert.ToString((int) (this.DataGridInfo.PageIndex + 1)) + "/" + this.DataGridInfo.PageCount.ToString() + "</b></font>&nbsp;页&nbsp;";
            adapter.Dispose();
            selectConnection.Close();
            selectConnection.Dispose();
        }

        private string method_1(string string_0, string string_1)
        {
            string_0 = this.publicFun_0.CheckString(string_0);
            string_1 = this.publicFun_0.CheckString(string_1);
            string values = "0";
            if (string_1 != "C")
            {
                if (!(string_1 == "D"))
                {
                    values = this.publicFun_0.GetValues("select Count(*) as CountUser from ExamUser where Flag='" + string_1 + "' and PaperID=" + string_0, "CountUser");
                }
                else
                {
                    values = this.publicFun_0.GetValues("select Count(*) as CountUser from ExamUser where PaperID=" + string_0, "CountUser");
                }
            }
            else
            {
                values = this.publicFun_0.GetValues("select Count(*) as CountUser from ExamUser where (Flag='C' or Flag='D' or Flag='E') and PaperID=" + string_0, "CountUser");
            }
            if (values.Trim() == "")
            {
                values = "0";
            }
            return values;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.httpCookie_0 = new HttpCookie("ExamUserCode");
            this.httpCookie_0 = base.Request.Cookies["ExamUserCode"];
            this.httpCookie_1 = new HttpCookie("ExamUserName");
            this.httpCookie_1 = base.Request.Cookies["ExamUserName"];
            this.httpCookie_2 = new HttpCookie("ExamUserLevel");
            this.httpCookie_2 = base.Request.Cookies["ExamUserLevel"];
            this.httpCookie_3 = new HttpCookie("ExamDeptID");
            this.httpCookie_3 = base.Request.Cookies["ExamDeptID"];
            if ((this.httpCookie_0 != null) && !(this.httpCookie_0.Value == ""))
            {
                base.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                base.Response.Cache.SetNoStore();
                this.httpCookie_0.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_0.Value));
                this.httpCookie_1.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_1.Value));
                this.httpCookie_2.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_2.Value));
                this.httpCookie_3.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_3.Value));
                if (!base.IsPostBack)
                {
                    if (this.publicFun_0.ReturnPaperData() != "Yes")
                    {
                        ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheck", "alert('此版本暂未授权,请拔打(0)13870606104手机号进行授权！')", true);
                    }
                    else
                    {
                        this.txtQuery.Attributes.Add("onkeydown", "SubmitKeyClick('butQuery');");
                        this.ViewState["SortFiled"] = "PaperID";
                        this.ViewState["SortType"] = " Desc";
                        this.publicFun_0.LoadSubjectInfo(this.DDLSubject, this.httpCookie_0.Value, "Y", "B", this.httpCookie_3.Value);
                        this.DDLSubject.Items.Insert(0, new ListItem("--所有科目信息--", "0"));
                        this.method_0();
                    }
                }
            }
            else
            {
                base.Response.Write("<script language=javascript>top.location.href='../Index.htm';self.close();</script>");
            }
        }
    }
}

